#include <iostream>
#include <string>
#include <unistd.h>
#include "httplib.h"
#include "Log.hpp"
#include "add_contacts.pb.h"
using namespace httplib;
using namespace ns_log;

#define SERVER_IP "127.0.0.1"
#define PORT 8888
void BuildRequest(add_contact::AddContactRequest *req);
void Menu()
{
    std::cout << "-----------------------------------------------------" << std::endl
              << "--------------- 请选择对通讯录的操作 ----------------" << std::endl
              << "------------------ 1、新增联系⼈ --------------------" << std::endl
              << "------------------ 2、删除联系⼈ --------------------" << std::endl
              << "------------------ 3、查看联系⼈列表 ----------------" << std::endl
              << "------------------ 4、查看联系⼈详细信息 ------------" << std::endl
              << "------------------ 0、退出 --------------------------" << std::endl
              << "-----------------------------------------------------" << std::endl;
}
enum CHOOSE
{
    QUIT = 0,
    ADD_CONTACT,
    DEL,
    FIND_ONE,
    FIND_ALL
};
void BuildRequest(add_contact::AddContactRequest *req)
{
    std::cout<<"---------------新增联系人---------------"<<std::endl;
    std::cout<<"输入联系人姓名: ";
    std::string name;
    std::getline(std::cin, name);
    req->set_name(name);
    int age = 0;
    std::cout<<"输入联系人年龄: ";
    std::cin >> age;
    req->set_age(age);
    // 清空cin缓冲区
    std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
    // 新增电话
    for(int i = 1;true;i++)
    {
        std::cout<<"---请输入该联系人的电话"<<i<<"(只输入空格表示完成)"<<": ";
        std::string number;
        std::getline(std::cin,number);
        if(number.size() == 0) break;
        add_contact::AddContactRequest_Phone * phone = req->add_phone();
        phone->set_number(number);
        std::cout<<"--请输入该联系人电话类型:|1.HP--2.TEL|-----: ";
        int type;
        std::cin>>type;
        switch(type)
        {
            case 1: 
                phone->set_type(add_contact::AddContactRequest_Phone_PhoneType::AddContactRequest_Phone_PhoneType_MP);
                break;
            case 2:
                phone->set_type(add_contact::AddContactRequest_Phone_PhoneType::AddContactRequest_Phone_PhoneType_TEL);
                break;
        }
    }
     std::cout<<"---------------添加联系人成功---------------"<<std::endl;
}
void AddContact(Client *client)
{
    // 构造请求
    add_contact::AddContactRequest req;
    BuildRequest(&req);
    // 序列化请求
    std::string req_str;
    if (!req.SerializeToString(&req_str))
    {
        LOG(FATAL, "序列化请求失败!\n");
        return;
    }
    // 发起post调用
    Result res = client->Post("/contacts/add", req_str, "application/protobuf");
    if (!res)
    {
        std::string error;
        error += "/contacts/add 请求失败!,错误信息为: ";
        error += httplib::to_string(res.error());
        LOG(ERROR,"%s\n",error.c_str());
        return;
    }
    // 反序列化resp
    add_contact::AddContactResponse resp;
    bool flag = resp.ParseFromString(res->body);
    if(!flag && res->status != 200)
    {
        std::string error;
        error += "/contacts/add 请求失败!,错误信息为: ";
        error += httplib::to_string(res.error());
        LOG(ERROR,"%s\n",error.c_str());
    }
    else if(res->status != 200)
    {
        std::string error;
        error += "/contacts/add 请求失败!,错误信息为: ";
        error += httplib::to_string(res.error());
        LOG(ERROR,"%s\n",error.c_str());
    }
    else if(!resp.success())
    {
        std::string error;
        error += "/contacts/add 响应结果失败!,异常信息为: ";
        error += httplib::to_string(res.error());
        LOG(ERROR,"%s\n",error.c_str());
    }
    // 结果打印
    std::cout<<"新增联系成功 id为: "<<resp.uid()<<std::endl;
}
int main()
{
    Client client(SERVER_IP, PORT);
    Menu();
    int op = 0;
    std::cout << "请选择: ";
    std::cin >> op;
    std::cin.ignore(256, '\n');
    switch (op)
    {
    case QUIT:
        std::cout << "已退出程序!" << std::endl;
        return 0;
    case ADD_CONTACT:
        AddContact(&client);
        break;
    case DEL:
        break;
    case FIND_ONE:
        break;
    case FIND_ALL:
        break;
    }
    return 0;
}